package com.project.slsx.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.adapter.CrashRegisterAdapter;
import com.project.slsx.bean.BaseBean;
import com.project.slsx.bean.SwitchBean;
import com.project.slsx.utils.DialogUtils;
import com.project.slsx.utils.ParamsUtils;
import com.project.slsx.utils.SPUtils;
import com.project.slsx.widget.dialog.BaseNiceDialog;
import com.project.slsx.widget.dialog.ViewConvertListener;
import com.project.slsx.widget.dialog.ViewHolder;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 */
public class CashRegisterActivity extends BaseActivity {

    private static final String TAG = CashRegisterActivity.class.getSimpleName();
    private RecyclerView rv;
    private String tvSwitchMoney = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_cash_register;
    }

    @Override
    public void initView() {
        rv = findViewById(R.id.recyclerview);
    }

    @Override
    public void initData() {
        GridLayoutManager gm = new GridLayoutManager(mContext, 4);
        rv.setLayoutManager(gm);
        CrashRegisterAdapter adapter = new CrashRegisterAdapter(R.layout.layout_item_crashregister);
        rv.setAdapter(adapter);
        adapter.setNewData(getTitles());

        adapter.setOnItemClickListener((adapter1, view, position) -> {
            switch (position) {
                case 0:
                    startActivity(new Intent(mContext, CashActivity.class));
                    break;

                case 1:
                    //订单列表
                    startActivity(new Intent(mContext, OrderListActivity.class));
                    break;

                case 2:
                    //会员
                    startActivity(new Intent(mContext, VIPActivity.class));
                    break;

                case 3:
                    //交班
                    DialogUtils.showDialog(R.layout.dialog_switch, 300, 240, true, switchListener, getSupportFragmentManager());
                    break;

                case 4:
                    //新盟商
                    startActivity(new Intent(mContext, NewShangMengActivity.class));
                    break;
                case 5:
                    //采购
                    startActivity(new Intent(mContext, PurchaseActivity.class));
                    break;
                case 6:
                    //采购
                    startActivity(new Intent(mContext, FastPurchaseActivity.class));
                    break;

                case 7:
                    //入库
                    startActivity(new Intent(mContext, StorageActivity.class));
                    break;

                case 8:
                    //商品损耗
                    startActivity(new Intent(mContext, GoodsLossActivity.class));
                    break;


                default:

                    break;
            }
        });
    }

    private ViewConvertListener switchListener = new ViewConvertListener() {
        @Override
        protected void convertView(ViewHolder holder, BaseNiceDialog dialog) {
            LinearLayout convertView = (LinearLayout) holder.getConvertView();
            EditText edSwitchMoney = convertView.findViewById(R.id.ed_switch_money);
            edSwitchMoney.addTextChangedListener(new MyWatcher());

            convertView.findViewById(R.id.btn_switch_ok).setOnClickListener(v -> {
                if (TextUtils.isEmpty(tvSwitchMoney)) {
                    showToast(mContext, "请输入金额");
                    return;
                }
                HttpParams shiftParams = ParamsUtils.getShiftParams();
                shiftParams.put("money", tvSwitchMoney);
                doShift(shiftParams);
            });

            convertView.findViewById(R.id.btn_switch_cancel).setOnClickListener(v -> {
                DialogUtils.dissmissDialog();
            });
        }
    };

    /**
     * 交班
     *
     * @param shiftParams
     */
    private void doShift(HttpParams shiftParams) {
        OkGo.<String>post(Constans.BASEURL).tag("shift").params(shiftParams).execute(new StringCallback() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
                super.onStart(request);
            }

            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                SwitchBean switchBean = JSONObject.parseObject(response.body(), SwitchBean.class);
                showToast(mContext, switchBean.getErrmsg());
                tvSwitchMoney = "";

                if (switchBean.getErrno() == Constans.SUCCESS) {
                    startActivity(new Intent(mContext, LoginActivity.class));
                    SPUtils.getInstance().put(Constans.SWITCH, true);
                    finish();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                showToast(mContext, "网络错误");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUtils.dissmissDialog();
            }
        });
    }

    private List<String> getTitles() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("收银");
        strings.add("订单");
        strings.add("会员");
        strings.add("交班");
        strings.add("新商盟");
        strings.add("采购");
        strings.add("快速采购");
        strings.add("入库");
        strings.add("商品损耗");
        return strings;
    }

    private class MyWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            tvSwitchMoney = s.toString().trim();
        }
    }
}
